<template>
	<view class='uni-flex uni-row' style="-webkit-justify-content: space-between;justify-content: space-between;">
		<view class='uni-flex uni-row'>


			<button v-if='powers.includes(power.add.power)' class="text " type="primary" plain
				@click="addClick">新增</button>
			<button v-if="!hasLeftWin && powers.includes(power.remove.power)" class="text" type="primary"
				plain>删除</button>
			<button v-if="!hasLeftWin && powers.includes(power.edit.power)" class="text" type="primary" plain
				size="mini">修改</button>
			<button v-if="!hasLeftWin && powers.includes(power.show.power)" class="text" type="primary" plain
				size="mini">查看</button>
			<button class="text" type="primary" plain size="mini">刷新</button>
		</view>

		<view class='uni-flex uni-row' v-if="!hasLeftWin">
			<button class="text" type="primary" plain>打印</button>
			<uni-data-select class="print" :clear="false" v-model="printstyle" :localdata="printstyles"
				@change="changePrintStyles"></uni-data-select>
			<uni-data-select class="printreport" :clear="false" v-model="reportstyle.value" :localdata="reportstyles"
				@change="changeReportStyles"></uni-data-select>

		</view>
		<!-- <view class='uni-flex uni-row'>
		<button class="text" type="primary" plain >筛选条件</button>
		
		</view> -->
		<!-- <view class='uni-flex uni-row'>			
		
		</view> -->

	</view>
</template>

<script setup>
	import {
		computed,
		ref
	} from "vue";
	import store from "@/store/index.js"
	import {apiGetReportStylesPublic,apiGetPrintToPdfPublic} from "@/api/apis.js"
	import {requestConnectPrintTool} from "@/utils/request.js"
	
	const BASE_URL = store.state.baseUrl;
	const props = defineProps({
		power: {
			type: Object,
			required: true,
			default () {
				return {
					add: {},
					remove: {},
					edit: {},
					show: {}
				}
			}
		},
		fileds: {
			type: Object,
			required: true,
			default () {
				return []
			}
		},
		data: {
			type: Object,
			required: true,
			default () {
				return []
			}
		}
	})
	const hasLeftWin = computed(() => store.state.noMatchLeftWindow)
	const powers = computed(() => store.state.userPower)
	const printstyle = ref('');
	const printstyles = ref([{
			value: '0',
			text: "打印预览"
		},
		{
			value: '1',
			text: "报表设计"
		},
		{
			value: '2',
			text: "新增打印样式"
		},
		{
			value: '3',
			text: "删除专属样式"
		},
		{
			value: '4',
			text: "打开本地位置"
		},
	]);
	//获取报表服务器数据
	const reportstyles = ref([])
	const reportstyle = ref({});
	//定义data参数
	const queryParams =ref({
		report:props.power.show.power	
	})
	const getReportStylesPublic = async ()=>{
		
		let res;
		if(queryParams.value) res = await apiGetReportStylesPublic(queryParams.value);
		
		if (res.code==0){
			res.data.forEach((item)=>{
				reportstyles.value.push({
					value:item.id,
					text:item.data_label+' (-共用-)', // '专属'
					...item
				})
			})
			reportstyles.value.push({
				value:'dddeee',
				text:'dda'+' (-共用-)', // '专属'
			})
			reportstyle.value=reportstyles.value[0]			
		}
		
		
	}
	getReportStylesPublic()
	const getPrintToPdfPublic = async ()=>{
		
		let res;
		queryParams.value.reportstyleinfo=reportstyle.value
		if (props.data){
		
			queryParams.value.fileds_items=JSON.stringify(props.data) //获取当前文件的参数
		}
		console.log('bbb',queryParams.value)
		alert('bbb')
		alert(JSON.stringify(queryParams.value))
		alert('123')
		if(queryParams.value) res = await apiGetPrintToPdfPublic(queryParams.value);
		
		console.log('bbb3332',queryParams)
		let pdfurl = res.pdfurl
		
		// #ifdef H5			
		   window.location.href=BASE_URL+pdfurl 
		// #endif
		
		
	}
	
	const changeReportStyles = function(e) {
		// e===reportstyle.value
		console.log('changeReportStyles',e)
	}
	//改变pring样式
	const changePrintStyles = function(e) {
		// e===reportstyle.value
		
		console.log('changePrintStyles',e)
		console.log('reportstyle.value',reportstyle.value)
		if (e===0 || e==='0'){
			/*
			打印预览：1.判断是否有本地打印工具 
					有-> 1.1 判断是-共用-' 跳转到pdfurl，
					    1.2  ‘-专属-'     打开报表预览工具
					无-> 跳转到pdfurl
			
			*/
			isConnectPrintTool()
			console.log('是否连接上',res)
			// if (reportstyle.value.text.includes('-共用-')){
			// 	getPrintToPdfPublic()
				
			// }
		}
	}
	//验证是否联系到打印工具
	const isConnectPrintTool= async function(){
		res = await requestConnectPrintTool();
		console.log(res)
		// return await uni.request({
		// 	url:"http://127.0.0.1:5003/connect",
		// 	method:'GET',			
		// 	success: (res) => {	
		// 			//console.log('success',res)
		// 			return ture
					
		// 	},
		// 	fail: (res) => {
				
		// 		   //console.log('错误',res) 
		// 		return false
					
		// 	}
			
				
		//   });
		   
	}
	
	
	//增删改查
	const addClick = function() {

		if (typeof props.power.add.url === 'string') {
			uni.navigateTo({
				url: props.power.add.url
			})
		} else {
			uni.navigateTo({
				url: props.power.add.url.url + props.power.add.url.url_path
			})
		}

	};
</script>

<style lang="scss" scoped>
	.text {
		margin: 15rpx 10rpx;
		padding: 0 20rpx;
		background-color: #ebebeb;
		height: 70rpx;
		line-height: 70rpx;
		text-align: center;
		color: #777;
		font-size: 26rpx;
	}

	.print {
		min-width: 280rpx;
	}

	.printreport {
		min-width: 450rpx;
	}
</style>